/**
 * Created by xiewei on 15/八月/13.
 */
//截取url后缀
var dairyId = '';
var stop = null;
var imgDir = 'img/';
//获取邮编
getRegionCode();
var getRegionTimer = setInterval(function(){
    if(root){
        clearInterval(getRegionTimer);
    }else{
        getRegionCode();
    }
},50);

/**
 * 获取评论列表
 * @type {number}
 */
var page = 1;
var pageSize = 3;
var more = $('#more');
var getCommentList = function(){
    if(page > 1){
        loading();
    }
    var commReqData = {
        dairyId: dairyId,
        page: page,
        pageSize: pageSize
    }
    $.ajax({
        url:'/dog2/appSocial/getCommentList',
        data: paramCoding(commReqData),
        type: 'POST',
        dataType: 'json',
        success: function(res){
            var commSection = $('#shareCommSection');
            if(res.respCode == 0){
                if(res.totalCount > 0){
                    commSection.removeClass('hidden');
                    var commUl = $('#shareComms');
                    //移出more
                    more.remove();
                    //添加评论
                    var comms = res.results;
                    var index = 0;
                    if(page==1 && pageSize==15){
                        index = 3
                    }
                    for(var i = index; i < comms.length ; i ++){
                        var comm = comms[i];
                        var li = $('<li>'),article = $('<article>');
                        //头像
                        if(comm.uface){
                            $('<img>').addClass('img-circle').attr('src',comm.uface).appendTo(li);
                        }else{
                            $('<img>').addClass('img-circle').attr('src','img/'+iconImgs.master).appendTo(li);
                        }

                        $('<h1>').text(comm.uname).appendTo(article);
                        $('<time>').text(timeSettled(comm.dateTime)).appendTo(article);
                        var commContent = replaceStr(comm.content);
                        if(comm.replyUname){
                            //回复的评论
                            $('<pre>').addClass('emoji_p')
                                .html('回复<h2>'+comm.replyUname+'</h2>：'+commContent).appendTo(article);
                        }else{
                            $('<pre>').addClass('emoji_p').text(commContent).appendTo(article);
                        }
                        article.appendTo(li);
                        li.appendTo(commUl);
                        //表情转换
                        emojifyTo($('.emoji_p',article));
                    }
                    //将more添加回底部
                    if(res.totalCount<=3 || (page>1 && page*pageSize>=res.totalCount) || (page==1 && pageSize==15 && res.totalCount<15)){
                        more.children('a').text('没有更多评论了');
                        more.children('.icon-down').hide();
                    }else if(page==1 && pageSize==3){
                        more.children('a').text('显示全部评论');
                        more.on('click',function(){
                            page = 1;pageSize = 15;
                            more.off('click');
                            getCommentList();
                        });
                    }else{
                        page++;
                        more.children('a').text('点击获取更多评论');
                        more.on('click',function(){
                            more.off('click');
                            getCommentList();
                        });
                    }
                    more.appendTo(commUl);
                }
            }
            if(page>1){
                loaded();
            }
        }
    })
}
/**
 * 获取用户信息
 */
loading();
dairyId = getUrlParam('dairyId');
//获取用户信息
var dairyReqData = {
    dairyId: dairyId
}
$.ajax({
    url:'/dog2/appSocial/getDairyInfo',
    data: paramCoding(dairyReqData),
    type: 'GET',
    dataType: 'json',
    success: function(res){
        if(res.respCode==0){
            //展示页面信息
            $('#diaryHeader').removeClass('hidden');
            //用户信息
            if(res.face){
                $('#diaryFace').attr('src',res.face).attr('alt',res.uname);
            }else{
                $('#diaryFace').attr('src',imgDir+iconImgs.master).attr('alt',res.uname);
            }
            $('#diaryUname').text(res.uname);
            fillAddrInfo(res.uregionid,$('#diaryUaddress'));
            $('#diaryDateTime').text(timeSettled(res.dateTime));
            //日志内容
            var storyContent = replaceStr(res.content);
            var shareContent = $('#diaryContent');
            shareContent.text(storyContent);
            //日志图片\视频
            var shareImg = imgDir+'viptail_logo300.jpg';
            var photoDiv = $('#diaryPhotos');
            if(res.mediaList){
                //视频
                //<div style="background-image:url('...')">
                //  <img src='button_album_video_play.png' >
                //  <video preload="auto" poster="">
                //      <source src="" type="video/mp4">
                //  </video>
                //</div>
                var medias = res.mediaList;
                shareImg = medias[0].cover+imgSize.thumbnail;
                for(var i = 0; i < medias.length ; i ++){
                    var media = medias[i];
                    var divW = getPageWidth()-30;
                    var divH = divW*media.coverScale;
                    var div = $('<div>').addClass('video');
                    var imgW = divW*0.3;
                    var img = $('<img>').attr('src',imgDir+'button_album_video_play.png')
                        .css({marginTop: divH/2-imgW/2});
                    var video = $('<video>').attr('src',media.media)
                        .attr('poster',media.cover+imgSize.thumbnail).attr('data',media.id)
                        .attr('preload','auto').addClass('stop');
                    var p = $('<p>').text('您的浏览器不支持播放该视频:(').appendTo(video);
                    video.appendTo(div);
                    img.appendTo(div);
                    //点击播放\暂停
                    div.on('click',function(){
                        if(video.hasClass('stop')){
                            video[0].play();
                            video.removeClass('stop');
                            video.next().addClass('hidden');

                            var videoTimer = setInterval(function(){
                                if(video[0].paused || video[0].ended){
                                    video.next().removeClass('hidden');
                                    clearInterval(videoTimer);
                                }
                            },1000);
                        }else if(!ifMobile()){
                            video[0].pause();
                            video.addClass('stop');
                            video.next().removeClass('hidden');
                        }
                    });
                    video.on('click',function(e){
                        $(this)[0].play();
                        e.stopPropagation();
                    });
                    video.next().on('click',function(e){
                        video[0].play();
                        e.stopPropagation();
                    });
                    div.appendTo(photoDiv);
                }
            }
            if(res.photos){
                //图片
                var photos = res.photos;
                shareImg = photos[0].original+imgSize.thumbnail;
                for(var i = 0; i < photos.length ; i ++){
                    var photo = photos[i];
                    $('<img>').attr('src',photo.original+imgSize.thumbnail).appendTo(photoDiv);
                }
            }
            setShareInfo('分享了'+res.uname+'的一个动态',shareImg);
            //发布地址
            $('#diaryAddress').text(res.address);
            //频道
            if(res.channelList && res.channelList.length>0){
                var chanel = res.channelList[0];
                $('#diaryChannelImg').attr('src',chanel.icon);
                $('#diaryChannel').text(chanel.title);
            }
            //收藏 评论 点赞数
            if(res.collectCount){
                $('#diaryCollectCount').text(res.collectCount);
                $('#diaryCollectCount').parent().removeClass('hidden');
            }
            if(res.commentCount){
                $('#diaryCommentCount').text(res.commentCount);
                $('#diaryCommentCount').parent().removeClass('hidden');
            }
            if(res.praiseCount){
                $('#diaryPraiseCount').text(res.praiseCount);
                $('#diaryPraiseCount').parent().removeClass('hidden');
            }
            //表情转换
            emojifyTo(shareContent);
            $('#bottomImg').removeClass('hidden');
            //获取评论列表
            getCommentList(page);
        }else{
            showMessageAlert('未获取到日志详情');
        }
        loaded();
    }
})

